package com.ruoyi.wms.service.impl;

import java.util.List;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.MD5Utils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.wms.dto.StockProductDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.wms.mapper.StockProductMapper;
import com.ruoyi.wms.domain.StockProduct;
import com.ruoyi.wms.service.IStockProductService;

/**
 * 库存商品信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-10-24
 */
@Service
public class StockProductServiceImpl implements IStockProductService {
    @Autowired
    private StockProductMapper stockProductMapper;

    /**
     * 查询库存商品信息
     *
     * @param id 库存商品信息主键
     * @return 库存商品信息
     */
    @Override
    public StockProduct selectStockProductById(String id) {
        return stockProductMapper.selectStockProductById(id);
    }

    /**
     * 查询库存商品信息列表
     *
     * @param stockProduct 库存商品信息
     * @return 库存商品信息
     */
    @Override
    public List<StockProduct> selectStockProductList(StockProductDto stockProduct) {
        return stockProductMapper.selectStockProductList(stockProduct);
    }

    /**
     * 新增库存商品信息
     *
     * @param stockProduct 库存商品信息
     * @return 结果
     */
    @Override
    public int insertStockProduct(StockProduct stockProduct) {
        stockProduct.setId(MD5Utils.md5(stockProduct.getProductCode()));
        stockProduct.setCreateUser(SecurityUtils.getUsername());
        stockProduct.setCreateDate(DateUtils.getCurrentTime());
        stockProduct.setUpdateDate( DateUtils.getCurrentTime());

        return stockProductMapper.insertStockProduct(stockProduct);
    }

    /**
     * 修改库存商品信息
     *
     * @param stockProduct 库存商品信息
     * @return 结果
     */
    @Override
    public int updateStockProduct(StockProduct stockProduct) {

        int updateResult = 0;
        if (StringUtils.isEmpty(stockProduct.getId())) {
            updateResult = insertStockProduct(stockProduct);
        }
        stockProduct.setUpdateDate( DateUtils.getCurrentTime());
        updateResult = stockProductMapper.updateStockProduct(stockProduct);
        if (0 == updateResult) {
            updateResult = insertStockProduct(stockProduct);
        }


        return updateResult;


    }

    /**
     * 批量删除库存商品信息
     *
     * @param ids 需要删除的库存商品信息主键
     * @return 结果
     */
    @Override
    public int deleteStockProductByIds(String[] ids) {
        return stockProductMapper.deleteStockProductByIds(ids);
    }

    /**
     * 删除库存商品信息信息
     *
     * @param id 库存商品信息主键
     * @return 结果
     */
    @Override
    public int deleteStockProductById(String id) {
        return stockProductMapper.deleteStockProductById(id);
    }
}
